import 'package:cached_network_image/cached_network_image.dart';
import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/material.dart';
import 'package:ball_lion/entity/other/resource_entity.dart';
import 'package:ball_lion/resources/styles.dart';
import 'package:ball_lion/utils/utils.dart';
import 'package:ball_lion/utils/widget_utils.dart';

import 'package:ball_lion/resources/colours.dart';
import 'base/wqs_button.dart';
import 'base/wqs_text.dart';

class GameEntryWidget extends StatefulWidget {
  const GameEntryWidget(
      {super.key,
      required this.lbts,
      this.scroll = true,
      this.carouselController});

  final List<ResourceEntity> lbts;
  final bool scroll;
  final CarouselSliderController? carouselController;

  @override
  State<GameEntryWidget> createState() => _GameEntryWidgetState();
}

class _GameEntryWidgetState extends State<GameEntryWidget> {
  double page = 0;

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return _gameCell();
  }

  Widget _gameCell() {
    final lbts = widget.lbts;
    final double indWidth = (12 * lbts.length).toDouble();
    return Container(
      height: 91,
      decoration: BoxDecoration(
          color: Colors.white, borderRadius: BorderRadius.circular(8)),
      child: Column(
        children: [
          Expanded(child: LayoutBuilder(builder: (context, p1) {
            return CarouselSlider(
              carouselController: widget.carouselController,
              options: CarouselOptions(
                viewportFraction: 1,
                autoPlay: widget.scroll,
                aspectRatio: p1.maxWidth / p1.maxHeight,
                // enlargeCenterPage: true,
                // autoPlayInterval: const Duration(seconds: 4),
                onScrolled: (value) {
                  page = (value ?? 0) % lbts.length;
                  update();
                },
              ),
              items: [
                ...List.generate(
                    lbts.length, (index) => _gameEntryBtn(lbts[index], index))
              ],
            );
          })),
          if (lbts.length > 1)
            Container(
              alignment: Alignment.center,
              margin: const EdgeInsets.only(bottom: 4),
              child: Container(
                width: indWidth,
                clipBehavior: Clip.hardEdge,
                decoration: BoxDecoration(
                    color: Colours.greyF2,
                    borderRadius: BorderRadius.circular(2)),
                child: Row(
                  children: [
                    SizedBox(width: page * indWidth / widget.lbts.length),
                    Flexible(
                      child: Container(
                          decoration: BoxDecoration(
                              color: Colours.main,
                              borderRadius: BorderRadius.circular(8)),
                          width: 12,
                          height: 4),
                    ),
                  ],
                ),
              ),
            )
        ],
      ),
    );
  }

  Widget _gameEntryBtn(ResourceEntity lbt, int index) {
    return GestureDetector(
      onTap: () {
        Utils.onEvent('wd_ggl');
        lbt.doJump();
      },
      behavior: HitTestBehavior.translucent,
      child: Container(
        padding: const EdgeInsets.all(12),
        child: Row(
          children: [
            Container(
              width: 60,
              height: 60,
              decoration: BoxDecoration(borderRadius: BorderRadius.circular(6)),
              clipBehavior: Clip.hardEdge,
              child: CachedNetworkImage(
                imageUrl: lbt.imgUrl ?? "",
                errorWidget: (context, url, error) => Container(
                  color: const Color(0xFFD9D9D9),
                ),
                placeholder: (context, url) => Container(
                  color: const Color(0xFFD9D9D9),
                ),
              ),
            ),
            const SizedBox(width: 8),
            Expanded(
                child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                QsText(lbt.button ?? "",
                    fontSize: 16, color: Colours.text, maxLines: 1),
                const SizedBox(
                  height: 2,
                ),
                QsText(lbt.content ?? "",
                    fontSize: 12, color: Colours.grey99, maxLines: 1)
              ],
            )),
            const SizedBox(
              width: 5,
            ),
            QsButton(
              onPressed: () {
                // Utils.doRoute(lbt.href ?? "");
                // Get.to(WebGamePage(), arguments: lbt.href ?? '');
                // launchUrl(Uri.parse(lbt.href ?? ""),
                //     mode: LaunchMode.externalApplication);
                lbt.doJump();
              },
              text: "进入",
              strutStyle: Styles.centerStyle(fontSize: 13),
              textStyle:
                  const TextStyle(fontSize: 13, fontWeight: FontWeight.w400),
              foregroundColor: Colors.white,
              backgroundColor: Colours.main,
              width: 56,
              height: 24,
            )
          ],
        ),
      ),
    );
  }
}
